package com.k8.rpc.result;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @Author: k8
 * @CreateTime: 2025-03-28
 * @Version: 1.0
 */
public class ResultFuture implements Future<Object> {
    private final Future<?> future;
    private final FutureFunction function;

    public ResultFuture(Future<?> future, FutureFunction function) {
        this.future = future;
        this.function = function;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Override
    public boolean isDone() {
        return future.isDone();
    }

    @Override
    public Object get() throws InterruptedException, ExecutionException {
        return function.apply(future, -1, null);
    }

    @Override
    public Object get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return function.apply(future, timeout, unit);
    }
}
